﻿using Logistics.Manager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Logistics
{
    public partial class frmSAPExport : Form
    {
        private TC_OWI_FILE obj_tc_owi = new TC_OWI_FILE();
        private List<Model.TC_OWI_FILE> user_owi_ulist;
        private new_ExportData _new_exportData = new new_ExportData();
        private old_ExportData _old_exportData = new old_ExportData();
        private string _directoryPath;
        public frmSAPExport()
        {
            InitializeComponent();
        }

        private void frmSAPExport_Load(object sender, EventArgs e)
        {
            BindCmbData();
            dpDate.Enabled = true;
            dpDate.Value = DateTime.Now;
        }
        #region 绑定基础数据
        private void BindCmbData()
        {
            string l_user = MyForms.User.UserName;
            user_owi_ulist = obj_tc_owi.User_GetList(l_user);
            var tc_owi = new Model.TC_OWI_FILE();

            tc_owi.TC_OWI002 = "请选择";
            tc_owi.TC_OWI001 = null;
            user_owi_ulist.Insert(0, tc_owi);
            tc_owi_comb.DataSource = user_owi_ulist;
            tc_owi_comb.DisplayMember = "TC_OWI002";
            tc_owi_comb.ValueMember = "TC_OWI001";
            tc_owi_comb.SelectedIndex = 0;
        }
        #endregion

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (tc_owi_comb.SelectedIndex == 0)
            {
                MessageBox.Show("请选择物流公司", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string tc_owr009 = tc_owi_comb.SelectedValue.ToString();
            string tc_owr002 = txtOwr002.Text.Trim();
            string tc_owr006 = dpDate.LockUpdateChecked? dpDate.Value.ToString("yy/MM/dd") : null;
            string tc_owr007 = jhDate.LockUpdateChecked ? jhDate.Value.ToString("yy/MM/dd") : null;

            //判断是否是新物流
            var owiModel = user_owi_ulist.FirstOrDefault(m => m.TC_OWI001 == tc_owr009);
            if (string.IsNullOrEmpty(owiModel.TC_OWI007))
            {
                MessageBox.Show("物流公司未维护 新老物流导出样式");
                this.Close();
            }
            if (owiModel.TC_OWI007 == "1") //1.老物流 
                old_export(tc_owr009, tc_owr002, tc_owr006, tc_owr007);
            if (owiModel.TC_OWI007 == "2") // 2.新物流
                new_export(tc_owr009, tc_owr002, tc_owr006, tc_owr007);
            this.Close();
        }



        /// <summary>
        /// FTP到服务器
        /// </summary>
        private bool UpLoadFile(string username, string password, string ftpPath, string category, string delete_chk)
        {
            FtpHelper.Instance.UserName = username;
            FtpHelper.Instance.Password = password;
            FtpHelper.Instance.FtpPath = ftpPath;
            FtpHelper.Instance.CheckDirectory(category.Split('/').ToList(),ftpPath, null);
            var files = Directory.GetFiles(_directoryPath);
            foreach (var item in files)
            {
                try
                {
                    FtpHelper.Instance.Upload(category, item);
                    Logger.WriteLine($"{item} 上传成功");
                }
                catch (Exception ex)
                {
                    Logger.WriteLine($"{item} 上传失败；详细信息：" + ex.Message);
                }
            }
            var url = new Uri(ftpPath + category);
            var ftpFiles = FtpHelper.Instance.GetFileList(url);

            var result = new List<string>();
            foreach (var file in files)
            {
                var name = Path.GetFileName(file);
                var ftpFile = ftpFiles.FirstOrDefault(m => m.Contains(name));
                if (ftpFile != null)
                    result.Add(ftpFile);
            }
            if (result.Count() == files.Count())
            {
                foreach (var item in files)
                {
                    try
                    {
                        //删除
                        if (delete_chk == "Y")
                            File.Delete(item);
                    }
                    catch { }
                }
                Logger.WriteLine($"本次文件成功上传到服务器目录：{url.ToString()}");
            }
            return true;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (tc_owi_comb.SelectedIndex == 0)
            {
                MessageBox.Show("请选择物流公司", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string tc_owr009 = tc_owi_comb.SelectedValue.ToString();

            //判断是否是新物流
            var owiModel = user_owi_ulist.FirstOrDefault(m => m.TC_OWI001 == tc_owr009);
            if (string.IsNullOrEmpty(owiModel.TC_OWI007))
            {
                MessageBox.Show("物流公司未维护 新老物流导出样式");
                this.Close();
            }
            if (owiModel.TC_OWI007 == "1") //1.老物流 
            { }
            if (owiModel.TC_OWI007 == "2") // 2.新物流
            {
                string url = "ftp://eri:topdaily@192.168.80.7/WL_TEST/WMS/" + tc_owr009.ToString() + "/SEND/";
               //System.Diagnostics.Process.Start("iexplore.exe", url);
                System.Diagnostics.Process.Start("Explorer.exe",url);
            }

           
        }
    }
}
